package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.core.utils.RequestUtil;
import com.yuke.cloud.service.api.cmc.dto.CmsTopicDto;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsAppTopicMapper;
import com.yuke.cloud.service.cmc.entity.CmsAppTopic;
import com.yuke.cloud.service.cmc.entity.CmsAppTopicData;
import com.yuke.cloud.service.cmc.service.CmsAppTopicDataService;
import com.yuke.cloud.service.cmc.service.CmsAppTopicService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by wg on 2019/01/18.
 */
@Service
@Transactional
public class CmsAppTopicServiceImpl extends BaseService<CmsAppTopic> implements CmsAppTopicService {
    @Resource
    private CmsAppTopicMapper cmsAppTopicMapper;
    @Resource
    private CmsAppTopicDataService cmsAppTopicDataService;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public void deleteById(Long id) {
        CmsAppTopicData cmsAppTopicData = new CmsAppTopicData();
        cmsAppTopicData.setTopicId(id);
        cmsAppTopicDataService.delete(cmsAppTopicData);
        super.deleteById(id);
    }

    @Override
    public int save(CmsAppTopic record) {
        record.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        return super.save(record);
    }

    @Override
    public int update(CmsAppTopic entity) {
        entity.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        int count = super.update(entity);
        if (count > 0) {
            UploadFile uploadFile = new UploadFile();
            List<String> filePaths = new ArrayList<>();
            if (entity.getOldIconUrl() != null && !"".equals(entity.getOldIconUrl())) {
               filePaths.add(entity.getOldIconUrl());
            }
            if (entity.getOldTopicIconUrl() != null && !"".equals(entity.getOldTopicIconUrl())) {
                filePaths.add(entity.getOldTopicIconUrl());
            }
            if (filePaths.size() > 0) {
                uploadFile.setFilePaths(filePaths);
                opcFileFeignApi.filesDelete(uploadFile);
            }
        }
        return count;
    }

    @Override
    public void deleteByIds(String ids) {
        cmsAppTopicDataService.deleteByTopicIds(ids);
        List<CmsAppTopic> cmsAppTopics = cmsAppTopicMapper.selectByIds(ids);
        super.deleteByIds(ids);
        UploadFile uploadFile = new UploadFile();
        List<String> filePaths = new ArrayList<>();
        for (CmsAppTopic cmsAppTopic : cmsAppTopics) {
            if (cmsAppTopic.getIconUrl() != null)
                filePaths.add(cmsAppTopic.getIconUrl());
            if (cmsAppTopic.getTopicIconUrl() != null)
                filePaths.add(cmsAppTopic.getTopicIconUrl());
        }
        uploadFile.setFilePaths(filePaths);
        opcFileFeignApi.filesDelete(uploadFile);
    }

    @Override
    public int updateList(List<CmsAppTopic> cmsAppTopics) {
        int count = 0;
        for (CmsAppTopic cmsAppTopic : cmsAppTopics) {
            count += this.update(cmsAppTopic);
        }
        return count;
    }

    /*@Override
    public int updateList(List<CmsAppTopic> cmsAppTopics) {
        int count = cmsAppTopicMapper.updateList(cmsAppTopics);
        return count;
    }*/

    @Override
    public List<CmsAppTopic> getByTopicCondition(CmsAppTopic cmsAppTopic) {
        return cmsAppTopicMapper.getByTopicCondition(cmsAppTopic);
    }

    @Override
    public List<CmsAppTopic> getListDetailByCondition(CmsAppTopic cmsAppTopic) {
        return cmsAppTopicMapper.getListDetailByCondition(cmsAppTopic);
    }

    @Override
    public List<CmsTopicDto> getTopicInfoByTopicIds(List<Long> topicIds){
        return cmsAppTopicMapper.getTopicInfoByTopicIds(topicIds);
    }
}
